package com.example.allshengmedical.experimentnew.activity;

import java.io.File;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.R.string;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.experimentnew.bean.FileBean;
import com.example.allshengmedical.experimentnew.presenter.BaseInformationPresenter;
import com.example.allshengmedical.experimentnew.view.IBaseInformationView;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.FileManagerUtil;
import com.example.allshengmedical.util.LogUtil;
import com.example.allshengmedical.util.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
//新建基本信息界面

public class BaseInformationActivity extends BaseActivity implements IBaseInformationView, OnClickListener {

	public TextView experimentNameET, experimentIdEt, experimenterEt;

	public Spinner spinnerExperType, spinnerSampleType, spinnerReagent;
	public ArrayList<String> spinnerExperTypeData_list, spinnerSampleTypeData_list, spinnerReagentData_list;
	private ArrayAdapter<String> spinnerExperTypeArr_adapter, spinnerSampleTypeArr_adapter, spinnerReagentArr_adapter;

	private Spinner spinner_pass;
	private List<String> data_list_pass;
	private ArrayAdapter<String> arr_adapter_pass;

	private PopupWindow menuPopupWindow;
	private boolean menuboo;

	private BaseInformationPresenter baseInformationPresenter;

	private FileManagerUtil fileManagerUtil;

	public static BaseInformationActivity context;

	AllShengDialog allShengDialog;

	public static BaseInformationActivity getInstence() {
		return context;
	}

	@Override
	protected int setcontent() {
		return R.layout.activity_new_baseinformation;
	}

	@Override
	protected void initView() {
		context = this;

		fileManagerUtil = new FileManagerUtil();
		baseInformationPresenter = new BaseInformationPresenter(this);
		baseInformationPresenter.initAppHead();
		baseInformationPresenter.initBottomView();

		SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
		experimentIdEt.setText(sdfDate.format(new Date()) + "_001");
		baseInformationPresenter.chooseExpType();
		baseInformationPresenter.chooseSamType();
		baseInformationPresenter.chooseReagent();

		data_list_pass = new ArrayList<String>();
		data_list_pass.add("NONE");
		data_list_pass.add("FAM");
		data_list_pass.add("SYBR Green I");
		data_list_pass.add("ROX");
		data_list_pass.add("TEXAS-RED");
		data_list_pass.add("HEX");
		data_list_pass.add("JOE");
		data_list_pass.add("CY3");
		data_list_pass.add("TET");
		data_list_pass.add("CY5");

		// 适配器
		arr_adapter_pass = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list_pass);
		// 设置样式
		arr_adapter_pass.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_pass.setAdapter(arr_adapter_pass);
	}

	@Override
	protected void initData() {

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.experimentNameET:
			baseInformationPresenter.inputName();
			break;
		case R.id.experimentIdEt:
			baseInformationPresenter.inputId();
			break;
		case R.id.experimenterEt:
			baseInformationPresenter.inputExperimenter();
			break;
		// case R.id.spinnerExperType:
		//
		// break;
		// case R.id.spinnerSampleType:
		//
		// break;
		// case R.id.spinnerReagent:
		//
		// break;

		default:
			break;
		}

	}

	@Override
	public void initAppHead() {
		setActionBar();
		setSystemTime();

	}

	@Override
	public void initBottomView() {
		setBottomView();

	}

	@Override
	public void inputName() {
		Intent keyBoardIntent = new Intent(BaseInformationActivity.this, KeyboardActivity.class);

		keyBoardIntent.putExtra("folderName", experimentNameET.getText().toString());
		keyBoardIntent.putExtra("title", getText(R.string.experiment_name));

		startActivityForResult(keyBoardIntent, 101);

	}

	@Override
	public void inputId() {
		Intent keyBoardIntent = new Intent(BaseInformationActivity.this, KeyboardActivity.class);
		keyBoardIntent.putExtra("folderName", experimentIdEt.getText().toString());
		keyBoardIntent.putExtra("title", getText(R.string.experiment_id));

		startActivityForResult(keyBoardIntent, 102);
	}

	@Override
	public void inputExperimenter() {
		Intent keyBoardIntent = new Intent(BaseInformationActivity.this, KeyboardActivity.class);

		keyBoardIntent.putExtra("folderName", experimenterEt.getText()

				.toString());
		keyBoardIntent.putExtra("title", getText(R.string.experimenter));

		startActivityForResult(keyBoardIntent, 103);

	}

	@Override
	public void chooseExpType() {
		// experTypePopupWindow(v);

		// 第一个下拉菜单

		spinnerExperTypeData_list = new ArrayList<String>();
		spinnerExperTypeData_list.add(getResources().getString(R.string.baseinformationdialog_absolute_quantitation));

		spinnerExperTypeData_list
				.add(getResources().getString(R.string.baseinformationdialog_standard_curve_relative_quantitation));

		spinnerExperTypeData_list
				.add(getResources().getString(R.string.baseinformationdialog__ct_relative_quantitation));

		// 适配器
		spinnerExperTypeArr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				spinnerExperTypeData_list);
		// 设置样式
		spinnerExperTypeArr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerExperType.setAdapter(spinnerExperTypeArr_adapter);

	}

	@Override
	public void chooseSamType() {
		// sampleTypePopupWindow();

		spinnerSampleTypeData_list = new ArrayList<String>();
		spinnerSampleTypeData_list.add(getResources().getString(R.string.baseinformationdialog_cdna));

		spinnerSampleTypeData_list.add(getResources().getString(R.string.baseinformationdialog_gdna));

		spinnerSampleTypeData_list.add(getResources().getString(R.string.baseinformationdialog_rna));

		// 适配器
		spinnerSampleTypeArr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				spinnerSampleTypeData_list);
		// 设置样式
		spinnerSampleTypeArr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerSampleType.setAdapter(spinnerSampleTypeArr_adapter);

	}

	@Override
	public void chooseReagent() {
		spinnerReagentData_list = new ArrayList<String>();
		spinnerReagentData_list.add(getResources().getString(R.string.baseinformationdialog_taqman));

		spinnerReagentData_list.add(getResources().getString(R.string.baseinformationdialog_sybr));

		spinnerReagentData_list.add(getResources().getString(R.string.baseinformationdialog_other));

		// 适配器
		spinnerReagentArr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				spinnerReagentData_list);
		// 设置样式
		spinnerReagentArr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerReagent.setAdapter(spinnerReagentArr_adapter);
		spinnerReagent.setSelection(1);

	}

	// 设置actionBar
	public void setActionBar() {
		Button leftfirstBtn, leftsecondBtn, rightfirstBtn, rightsecondBtn, rightthirdBtn;
		TextView title;

		leftfirstBtn = BindView(R.id.leftfirst_btn);
		leftsecondBtn = BindView(R.id.leftsecond_btn);
		rightfirstBtn = BindView(R.id.rightfirst_btn);
		rightsecondBtn = BindView(R.id.rightsecond_btn);
		rightthirdBtn = BindView(R.id.rightthird_btn);

		title = BindView(R.id.title);

		leftfirstBtn.setVisibility(View.GONE);
		leftfirstBtn.setText(R.string.item_actionbar_menu);
		leftsecondBtn.setVisibility(View.VISIBLE);
		leftsecondBtn.setText(R.string.item_actionbar_home);
		rightfirstBtn.setVisibility(View.GONE);
		rightfirstBtn.setText(R.string.actionBar_new);
		rightsecondBtn.setVisibility(View.GONE);
		rightsecondBtn.setText(R.string.actionBar_saveAs);
		rightthirdBtn.setVisibility(View.GONE);
		rightthirdBtn.setText(R.string.actionBar_save);

		title.setText(R.string.baseInformationActivity_title);

		leftfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!menuboo) {
					showMenuPopupWindow(v);
					menuboo = !menuboo;
				} else {
					menuPopupWindow.dismiss();
					menuboo = !menuboo;
				}

			}

		});

		leftsecondBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				allShengDialog = new AllShengDialog(context, getString(R.string.dialog_tishi),
						getString(R.string.dialog_del), getString(R.string.dialog_yes), getString(R.string.dilog_no));

				allShengDialog.setClicklistener(new AllShengDialog.ClickListenerInterface() {

					@Override
					public void OK() {
						BaseApplication.getInstance().finishAll();
						Intent intent = new Intent(BaseInformationActivity.this, MainMenuActivity.class);
						startActivity(intent);
						allShengDialog.dismiss();
					}

					@Override
					public void Cancle() {
						allShengDialog.dismiss();
					}

				});
				allShengDialog.show();
				// BaseApplication.getInstance().finishAll();
				// Intent intent = new Intent(BaseInformationActivity.this,
				// MainMenuActivity.class);
				// startActivity(intent);
			}
		});

		rightfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ByAlert.alert("New");
				Intent intent = new Intent(BaseInformationActivity.this, FileManagerActivity.class);
				startActivity(intent);
			}
		});
		rightsecondBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ByAlert.alert("Save As");
				Intent intent = new Intent(BaseInformationActivity.this, FileManagerActivity.class);
				startActivity(intent);
			}
		});
		rightthirdBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				savePopupWindow(v);

			}

		});

	}

	// 获取当前时间的方法
	public void setSystemTime() {

		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText(SharedPreferencesUtils.get(this, BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "") + "/"
				+ SharedPreferencesUtils.get(this, BaseApplication.FILEMANGER_PREPAREFILENAME, ""));

	}

	// 设置bottom布局
	public void setBottomView() {
		Button bottomLeftFistBtn, bottomCenterBtn, bottomRightBtn;

		bottomLeftFistBtn = BindView(R.id.bottomLeftFistBtn);
		bottomCenterBtn = BindView(R.id.bottomCenterBtn);
		bottomRightBtn = BindView(R.id.bottomRightBtn);

		bottomLeftFistBtn.setVisibility(View.VISIBLE);
		// bottomCenterBtn.setVisibility(View.VISIBLE);
		bottomRightBtn.setVisibility(View.VISIBLE);

		bottomLeftFistBtn.setText(R.string.bottomView_back);
		// TODO 这个button的文字不确定暂时用A代替
		bottomCenterBtn.setText("A"); // 不确定！！！！！！！
		bottomRightBtn.setText(R.string.bottomView_next);
		bottomLeftFistBtn.setVisibility(View.GONE);
		bottomLeftFistBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ByAlert.alert("back");
				finish();
				Intent intent = new Intent(BaseInformationActivity.this, ManagerActivity1.class);
				startActivity(intent);

			}
		});

		bottomCenterBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ByAlert.alert("A");

			}
		});
		bottomRightBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!whetherNull()) {
					return;
				}

				if ("SYBR".equals(spinnerReagent.getSelectedItem())) {
					if ("FAM".equals(spinner_pass.getSelectedItem())) {

						final AllShengDialog allShengDialog = new AllShengDialog(BaseInformationActivity.this, "",
								"Dye \"FAM\" already used in passive reference", "确认");
						allShengDialog.setClicklistener(new AllShengDialog.ClickListenerInterface() {

							@Override
							public void OK() {
								// TODO Auto-generated method stub
								allShengDialog.dismiss();
							}

							@Override
							public void Cancle() {
								// TODO Auto-generated method stub
								allShengDialog.dismiss();
							}

						});
						allShengDialog.show();

					}
					if ("SYBR Green I".equals(spinner_pass.getSelectedItem())) {
						final AllShengDialog allShengDialog = new AllShengDialog(BaseInformationActivity.this, "",
								"Dye \"SYBR\" already used in passive reference", "确认");
						allShengDialog.setClicklistener(new AllShengDialog.ClickListenerInterface() {

							@Override
							public void OK() {
								// TODO Auto-generated method stub
								allShengDialog.dismiss();
							}

							@Override
							public void Cancle() {
								// TODO Auto-generated method stub
								allShengDialog.dismiss();
							}

						});
						allShengDialog.show();

					}

					if ("SYBR Green I".equals(spinner_pass.getSelectedItem())
							|| "FAM".equals(spinner_pass.getSelectedItem())) {
						return;
					}
				}

				// 创建文件
				String folderName = SharedPreferencesUtils.get(BaseInformationActivity.this,
						BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "0") + "";
				String fileName = SharedPreferencesUtils.get(BaseInformationActivity.this,
						BaseApplication.FILEMANGER_PREPAREFILENAME, "0") + "";
				StringBuffer content = new StringBuffer();
				content.append("ExperimentName:" + experimentNameET.getText());
				content.append("\r\n" + "ExperimentID:" + experimentIdEt.getText());
				content.append("\r\n" + "Experimenter:" + experimenterEt.getText());
				content.append("\r\n" + "ExperimentType:"
						+ spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()));
				content.append("\r\n" + "SampleType:"
						+ spinnerSampleTypeData_list.get(spinnerSampleType.getSelectedItemPosition()));
				content.append(
						"\r\n" + "Reagent:" + spinnerReagentData_list.get(spinnerReagent.getSelectedItemPosition()));
				content.append("\r\n" + "PassReference:" + data_list_pass.get(spinner_pass.getSelectedItemPosition()));

				fileManagerUtil.createFIle(folderName, fileName);
				fileManagerUtil.writeFile(folderName, fileName, content + "", false);

				SharedPreferencesUtils.put(BaseInformationActivity.this, BaseApplication.BASEINFORMATION_EXPERIMENTTYPE,
						spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()) + "");

				SharedPreferencesUtils.put(BaseInformationActivity.this, BaseApplication.BASEINFORMATION_REAGENT,
						spinnerReagentData_list.get(spinnerReagent.getSelectedItemPosition()));

//				freeAllObject();

				Intent intent = new Intent(BaseInformationActivity.this, LayoutOneActivity.class);
				intent.putExtra("pass", data_list_pass.get(spinner_pass.getSelectedItemPosition()));
				startActivity(intent);

				ArrayList<FileBean> fileBeans = new ArrayList<FileBean>();

				// 读取
				SharedPreferences preferences = getSharedPreferences(BaseApplication.FILEMANGER_HISTORYARRAYLIST,
						MODE_PRIVATE);
				String json = preferences.getString("fileBean", null);

				if (json != null) {
					Gson gson = new Gson();
					Type type = new TypeToken<List<FileBean>>() {
					}.getType();

					fileBeans = gson.fromJson(json, type);
					for (int i = 0; i < fileBeans.size(); i++) {
						Log.d("读sp", fileBeans.get(i).getFoldName() + ":" + fileBeans.get(i).getFileName() + ","
								+ fileBeans.get(i).getCreateTime());
					}
				}

				String chosenFilePath = Environment.getExternalStorageDirectory() + "/" + "AllShengMedical" + "/"
						+ folderName + "/" + fileName;

				File chosenFile = new File(chosenFilePath);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long time = chosenFile.lastModified();

				FileBean fileBean = new FileBean(fileName, format.format(new Date(time)), "", folderName);

				// 遍历现在加的文件历史有没有，如果有就不加，没有加如集合。
				// 还要判断文件集合 的size ，如果集合大于20就要删除最后一个。再添加
				boolean spFileBoo = false;
				for (FileBean file : fileBeans) {
					if (fileBean.getFileName().equals(file.getFileName())) {
						if (fileBean.getFoldName().equals(file.getFoldName())) {
							spFileBoo = true;
						} else {

						}
					} else {

					}
				}
				if (!spFileBoo) {
					if (fileBeans.size() == 20) {
						fileBeans.remove(19);
					}
					fileBeans.add(0, fileBean);
				}

				// SharedPreferencesUtils.get(BaseInformationActivity.this,
				// BaseApplication.FILEMANGER_HISTORYARRAYLIST, "");

				// 如果名字有相同的就不加进去

				// 写
				SharedPreferences.Editor editor = getSharedPreferences(BaseApplication.FILEMANGER_HISTORYARRAYLIST,
						MODE_PRIVATE).edit();
				Gson gson = new Gson();
				String json2 = gson.toJson(fileBeans);
				Log.d("gson写", "saved json is " + json2);
				editor.putString("fileBean", json2);
				editor.commit();

			}

			private void freeAllObject() {
				experimentNameET = null;
				experimentIdEt = null;
				experimenterEt = null;

				spinnerExperType = null;
				spinnerSampleType = null;
				spinnerReagent = null;
				spinnerExperTypeData_list = null;
				spinnerSampleTypeData_list.clear();
				spinnerReagentData_list.clear();
				spinnerExperTypeArr_adapter = null;
				spinnerSampleTypeArr_adapter = null;
				spinnerReagentArr_adapter = null;

				spinner_pass = null;
				data_list_pass.clear();
				arr_adapter_pass = null;

				menuPopupWindow = null;

				baseInformationPresenter = null;

				fileManagerUtil = null;

				context = null;

				allShengDialog = null;
				System.gc();

			}
		});
	}

	// menu的弹出窗口
	private void showMenuPopupWindow(View v) {

		if (null == menuPopupWindow || !menuPopupWindow.isShowing()) {
			View contentView = LayoutInflater.from(this).inflate(R.layout.fragment_new_basicinformation_menu, null);

			menuPopupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			menuPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			menuPopupWindow.setTouchable(true);
			menuPopupWindow.setOutsideTouchable(true);
			menuPopupWindow.showAsDropDown(v, -40, -3);

			TextView basicInformationTv, layoutTv, methodTv, previewTv;
			basicInformationTv = (TextView) contentView.findViewById(R.id.basicInformationTv);
			layoutTv = (TextView) contentView.findViewById(R.id.layoutTv);
			methodTv = (TextView) contentView.findViewById(R.id.methodTv);
			previewTv = (TextView) contentView.findViewById(R.id.previewTv);

			basicInformationTv.setBackground(getResources().getDrawable(R.color.darkBlue));
			basicInformationTv.setTextColor(this.getResources().getColor(R.color.white));
			layoutTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					menuPopupWindow.dismiss();

				}
			});
			methodTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// Intent intent =new Intent(BaseInformationActivity, uri)
					ByAlert.alert("跳转页面method");
					menuPopupWindow.dismiss();
				}
			});
			previewTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ByAlert.alert("跳转页面preview");
					menuPopupWindow.dismiss();
				}
			});

		} else {
			menuPopupWindow.dismiss();
			menuboo = !menuboo;
		}

	}

	// experType的popwindow配置
	private void experTypePopupWindow(View v) {

		View conteView = LayoutInflater.from(this).inflate(R.layout.dialog_new_baseimformation_expertype, null);

		TextView scRelativeTv, ctRelativeTv, absoluteTv;
		scRelativeTv = (TextView) conteView.findViewById(R.id.scRelativeTv);
		ctRelativeTv = (TextView) conteView.findViewById(R.id.ctRelativeTv);
		absoluteTv = (TextView) conteView.findViewById(R.id.absoluteTv);

		if (spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()).equals(scRelativeTv.getText())) {
			scRelativeTv.setBackground(getResources().getDrawable(R.color.darkBlue));
			scRelativeTv.setTextColor(getResources().getColor(R.color.white));
		}
		if (spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()).equals(absoluteTv.getText())) {
			absoluteTv.setBackground(getResources().getDrawable(R.color.darkBlue));
			absoluteTv.setTextColor(getResources().getColor(R.color.white));
		}
		if (spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()).equals(ctRelativeTv.getText())) {
			ctRelativeTv.setBackground(getResources().getDrawable(R.color.darkBlue));
			ctRelativeTv.setTextColor(getResources().getColor(R.color.white));
		}

		final PopupWindow experTypePopupWindow = new PopupWindow(conteView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);
		// scRelativeTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerExperType
		// .setText(R.string.baseinformationdialog_standard_curve_relative_quantitation);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// absoluteTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerExperType
		// .setText(R.string.baseinformationdialog_absolute_quantitation);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// ctRelativeTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerExperType
		// .setText(R.string.baseinformationdialog__ct_relative_quantitation);
		// experTypePopupWindow.dismiss();
		// }
		// });

		experTypePopupWindow.setTouchable(true);

		experTypePopupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}

		});

		experTypePopupWindow.setBackgroundDrawable(new BitmapDrawable());
		experTypePopupWindow.setTouchable(true);
		experTypePopupWindow.setOutsideTouchable(true);
		experTypePopupWindow.showAtLocation(v, Gravity.CENTER, 0, -15);

	}

	// reagent的PopupWindow配置
	private void reagentPopupWindow(View v) {
		View conteView = LayoutInflater.from(this).inflate(R.layout.dialog_new_baseimformation_reagents, null);

		TextView taqmanTv, SybrTv, otherTv;
		taqmanTv = (TextView) conteView.findViewById(R.id.taqmanTv);
		SybrTv = (TextView) conteView.findViewById(R.id.SybrTv);
		otherTv = (TextView) conteView.findViewById(R.id.otherTv);
		// if (spinnerReagent.getText().equals(taqmanTv.getText())) {
		// taqmanTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// taqmanTv.setTextColor(getResources().getColor(R.color.white));
		// }
		// if (spinnerReagent.getText().equals(SybrTv.getText())) {
		// SybrTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// SybrTv.setTextColor(getResources().getColor(R.color.white));
		// }
		// if (spinnerReagent.getText().equals(otherTv.getText())) {
		// otherTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// otherTv.setTextColor(getResources().getColor(R.color.white));
		// }

		final PopupWindow experTypePopupWindow = new PopupWindow(conteView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);
		// taqmanTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerReagent.setText(R.string.baseinformationdialog_taqman);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// SybrTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerReagent.setText(R.string.baseinformationdialog_sybr);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// otherTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerReagent.setText(R.string.baseinformationdialog_other);
		// experTypePopupWindow.dismiss();
		// }
		// });

		experTypePopupWindow.setTouchable(true);

		experTypePopupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}

		});

		experTypePopupWindow.setBackgroundDrawable(new BitmapDrawable());
		experTypePopupWindow.setTouchable(true);
		experTypePopupWindow.setOutsideTouchable(true);
		experTypePopupWindow.showAtLocation(v, Gravity.CENTER, 0, 75);
	}

	// sampletype的popwindow配置
	private void sampleTypePopupWindow(View v) {
		View conteView = LayoutInflater.from(this).inflate(R.layout.dialog_new_baseimformation_sampletype, null);

		TextView cDnaTv, gDnaTv, rnaTv;
		cDnaTv = (TextView) conteView.findViewById(R.id.cDnaTv);
		gDnaTv = (TextView) conteView.findViewById(R.id.gDnaTv);
		rnaTv = (TextView) conteView.findViewById(R.id.rnaTv);

		// if (spinnerSampleType.getText().equals(cDnaTv.getText())) {
		// cDnaTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// cDnaTv.setTextColor(getResources().getColor(R.color.white));
		// }
		// if (spinnerSampleType.getText().equals(gDnaTv.getText())) {
		// gDnaTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// gDnaTv.setTextColor(getResources().getColor(R.color.white));
		// }
		// if (spinnerSampleType.getText().equals(rnaTv.getText())) {
		// rnaTv.setBackground(getResources().getDrawable(R.color.darkBlue));
		// rnaTv.setTextColor(getResources().getColor(R.color.white));
		// }

		final PopupWindow experTypePopupWindow = new PopupWindow(conteView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);
		// cDnaTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerSampleType.setText(R.string.baseinformationdialog_cdna);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// gDnaTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerSampleType.setText(R.string.baseinformationdialog_gdna);
		// experTypePopupWindow.dismiss();
		// }
		// });
		// rnaTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// spinnerSampleType.setText(R.string.baseinformationdialog_rna);
		// experTypePopupWindow.dismiss();
		// }
		// });

		experTypePopupWindow.setTouchable(true);

		experTypePopupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}

		});

		experTypePopupWindow.setBackgroundDrawable(new BitmapDrawable());
		experTypePopupWindow.setTouchable(true);
		experTypePopupWindow.setOutsideTouchable(true);
		experTypePopupWindow.showAtLocation(v, Gravity.CENTER, 0, 135);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		// 点击实验名称编辑，返回时传数据
		if (requestCode == 101 && resultCode == 201) {
			experimentNameET.setText(data.getStringExtra("result"));
		}
		// 点击实验名称编辑，返回时不传数据
		if (requestCode == 101 && resultCode == 202) {

		}
		// 点击实验ID编辑，返回时传数据
		if (requestCode == 102 && resultCode == 201) {
			experimentIdEt.setText(data.getStringExtra("result"));
		}
		// 点击文实验ID编辑，返回时传不数据
		if (requestCode == 102 && resultCode == 202) {

		}
		// 点击实验人员编辑，返回传值成功
		if (requestCode == 103 && resultCode == 201) {
			experimenterEt.setText(data.getStringExtra("result"));
		}
		// 点击实验人员编辑，返回传值失败
		if (requestCode == 103 && resultCode == 202) {

		}

	}

	private void savePopupWindow(View v) {
		View conteView = LayoutInflater.from(this).inflate(R.layout.dialog_baseinformation_save, null);

		Button confirmBtn, cancelBtn;

		confirmBtn = (Button) conteView.findViewById(R.id.confirm_btn);
		cancelBtn = (Button) conteView.findViewById(R.id.cancel_btn);

		final PopupWindow popupWindow = new PopupWindow(conteView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				true);

		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}

		});

		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setTouchable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

		confirmBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!whetherNull()) {
					// ByAlert.alert("保存失败");
					final AllShengDialog allShengDialog = new AllShengDialog(BaseInformationActivity.this, "", "保存失败",
							"OK");
					allShengDialog.show();
					allShengDialog.setClicklistener(new ClickListenerInterface() {

						@Override
						public void OK() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();
							return;
						}

						@Override
						public void Cancle() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();
						}

					});

					popupWindow.dismiss();
					return;
				}
				// 创建文件
				String folderName = SharedPreferencesUtils.get(BaseInformationActivity.this,
						BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "0") + "";
				String fileName = SharedPreferencesUtils.get(BaseInformationActivity.this,
						BaseApplication.FILEMANGER_PREPAREFILENAME, "0") + "";
				StringBuffer content = new StringBuffer();
				content.append("ExperimentName:" + experimentNameET.getText());
				content.append("\r\n" + "ExperimentID:" + experimentIdEt.getText());
				content.append("\r\n" + "Experimenter:" + experimenterEt.getText());
				content.append("\r\n" + "ExperimentType:"
						+ spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()));
				content.append("\r\n" + "SampleType:"
						+ spinnerSampleTypeData_list.get(spinnerSampleType.getSelectedItemPosition()));
				content.append(
						"\r\n" + "Reagent:" + spinnerReagentData_list.get(spinnerReagent.getSelectedItemPosition()));

				fileManagerUtil.createFIle(folderName, fileName);
				fileManagerUtil.writeFile(folderName, fileName, content + "", false);

				popupWindow.dismiss();
			}

		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});

	}

	// 判定6个文字框是否为空
	private boolean whetherNull() {
		if (TextUtils.isEmpty(experimentNameET.getText())) {
			ByAlert.alert(getString(R.string.absolutelayout_fillit));
			return false;
		}
		if (TextUtils.isEmpty(experimentIdEt.getText())) {
			ByAlert.alert(getString(R.string.absolutelayout_fillit));
			return false;
		}
		if (TextUtils.isEmpty(experimenterEt.getText())) {
			ByAlert.alert(getString(R.string.absolutelayout_fillit));
			return false;
		}
		if (TextUtils.isEmpty(spinnerExperTypeData_list.get(spinnerExperType.getSelectedItemPosition()))) {
			ByAlert.alert(getString(R.string.absolutelayout_fillit));
			return false;
		}
		// if (TextUtils.isEmpty(spinnerSampleType.getText())) {
		// ByAlert.alert("请补全信息");
		// return false;
		// }
		// if (TextUtils.isEmpty(spinnerReagent.getText())) {
		// ByAlert.alert("请补全信息");
		// return false;
		// }
		return true;
	}

}
